package com.zhao.dao.primary;

import com.zhao.entity.primary.Body;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

@Mapper
@Component
public interface BodyDao{

	//插入
	@InsertProvider(type = SqlProvider.class,method = "insert")
	Integer insert(Body record);

	//更新
	@UpdateProvider(type = SqlProvider.class,method = "update")
	Integer update(Body record);

	//删除
	@DeleteProvider(type = SqlProvider.class,method = "delete")
	Integer delete(Body record);

	class SqlProvider{

		public static String insert(final Body record){
			return new SQL(){{
				INSERT_INTO("BODY");
				VALUES("DATA_ID", "#{dataId}");
				VALUES("DATA_BODY", "#{dataBody}");
				VALUES("CREATE_TIME", "#{createTime}");
				VALUES("VERSION", "#{version}");
			}}.toString();
		}

		public static String update(final Body record){
			return new SQL(){{
				UPDATE("BODY");
				if(record.getDataBody()!=null){
					SET("DATA_BODY=#{dataBody}");
				}
				if(record.getVersion()!=null){
					SET("VERSION=#{version}");
				}
				WHERE("DATA_ID=#{dataId}","CREATE_TIME=#{createTime}");
			}}.toString();
		}

		public static String delete(final Body record){
			return new SQL(){{
				DELETE_FROM("BODY");
				WHERE("DATA_ID=#{dataId}","CREATE_TIME=#{createTime}");
			}}.toString();
		}
	}

}